package cn.com.dealme.error;

import java.util.Map;

import org.nutz.aop.InterceptorChain;
import org.nutz.aop.interceptor.AbstractMethodInterceptor;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import cn.com.dealme.Keys;
import cn.com.dealme.nutz.view.DJsonView;

/**
 * 这个过滤器将过滤所有Module的所有方法
 * 一但 catch 到下层未 catch 的异常刚返回服务器错误报告
 * 
 * @author zzm 2011-05-05
 */
public class ServerExceptionCatchInterceptor extends AbstractMethodInterceptor {
	
	private static final Log log = Logs.getLog(ServerExceptionCatchInterceptor.class);

	@Override
	public void filter(InterceptorChain chain) throws Throwable {
		try {
			chain.doChain();
		} catch (Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e, e);
				log.error(e.getCause(), e.getCause());
			}
			// 服务器错误报告
			Map<String, String> map = ErrorMap.makeErrMap();
			map.put(Keys.KeyServerError, ErrMsgManager
					.getResource(Keys.ServerError)
					+ e.getClass().getName() + " " + e.getMessage());
			chain.setReturnValue(new DJsonView(map));
		}

	}

}
